package com.zz.service.iml;

import com.zz.dao.InDao;
import com.zz.dao.PatientDao;
import com.zz.dao.WardDao;
import com.zz.dto.GetBed;
import com.zz.dto.PatientLiveIn;
import com.zz.dto.PatientResult;
import com.zz.pojo.In;
import com.zz.pojo.Patient;
import com.zz.pojo.Ward;
import com.zz.service.PatientService;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Copyright Zz
 * FileName: PatientServiceIml
 * Author:   Zz
 * Date:     2023/6/512:16
 * Description:在院病人业务层实现类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
@Service
@Transactional
public class PatientServiceIml implements PatientService {
    @Autowired
    private PatientDao patientDao;

    @Autowired
    private WardDao wardDao;

    @Autowired
    private InDao inDao;

    /**
     * queryPatientListOrderByTime和queryPatientListByNumber的公共代码区域
     *
     * @param list 从数据库中查出来的Patient数据
     * @return PatientResult所需第一个字段
     */
    private List<PatientLiveIn> CommonMethod(List<Patient> list) {
        List<PatientLiveIn> result = new ArrayList<>();

        Iterator<Patient> it = list.iterator();

        while (it.hasNext()) {
            Patient patient = it.next();

            In bed = patient.getBed();
            String number = bed.getAreaCode() + "-" + bed.getRoomNumber() + "-" + bed.getBedNumber();

            result.add(new PatientLiveIn(patient.getPatientId(),
                    patient.getPatientName(),
                    patient.getPatientSex(),
                    patient.getPatientAge(),
                    patient.getPatientInTime(),
                    patient.getPatientWhy(),
                    number
            ));
        }
        return result;
    }

    @Override
    public PatientResult queryPatientListOrderByTime(Map<String, Integer> map) {
        List<Patient> list = patientDao.queryPatientListOrderByTime(map);


        List<PatientLiveIn> result = CommonMethod(list);

        int countPatientNumber = patientDao.countPatientNumber();

        int countPageNumber = (int) Math.ceil(countPatientNumber / (double) Integer.parseInt(String.valueOf(map.get("pageSize"))));

        return new PatientResult(result, countPageNumber, result.size());

    }

    @Override
    public PatientResult queryPatientListByNumber(Map<String, Object> map) {
        List<Patient> list = patientDao.queryPatientListByNumber(map);

        List<PatientLiveIn> result = CommonMethod(list);

        int countPatientNumber = patientDao.countPatientByNumber(map);
        int countPageNumber = (int) Math.ceil(countPatientNumber / (double) Integer.parseInt(String.valueOf(map.get("pageSize"))));

        return new PatientResult(result, countPageNumber, result.size());
    }

    @Override
    public GetBed insertPatient(PatientLiveIn patientLiveIn){
        // 查询此病人是否住院
        Patient patient = patientDao.queryPatientByID(patientLiveIn.getId());
        int bedNumber;
        if (patient == null) {
            String[] s = patientLiveIn.getNumber().split("-");
            Ward ward = wardDao.queryWardByAreaCode_RoomNumber(s[0], s[1]);
            if (ward != null) {
                if (ward.getNumbers() > 0) {
                    if (ward.isEmpty1()) {
                        ward.setNumbers(ward.getNumbers() - 1);
                        ward.setEmpty1(false);
                        wardDao.updateWard(ward);
                        In in = new In(patientLiveIn.getId(),
                                ward.getAreaCode(),
                                ward.getRoomNumber(),
                                "1");
                        inDao.insertIn(in);
                        bedNumber=1;
                    } else if (ward.isEmpty2()) {
                        ward.setNumbers(ward.getNumbers() - 1);
                        ward.setEmpty2(false);
                        wardDao.updateWard(ward);
                        In in = new In(patientLiveIn.getId(),
                                ward.getAreaCode(),
                                ward.getRoomNumber(),
                                "2");
                        inDao.insertIn(in);
                        bedNumber=2;
                    } else if (ward.isEmpty3()) {
                        ward.setNumbers(ward.getNumbers() - 1);
                        ward.setEmpty3(false);
                        wardDao.updateWard(ward);
                        In in = new In(patientLiveIn.getId(),
                                ward.getAreaCode(),
                                ward.getRoomNumber(),
                                "3");
                        inDao.insertIn(in);
                        bedNumber=3;
                    } else return new GetBed(4,0);// 房间已满
                } else return new GetBed(4,0);// 房间已满
            } else return new GetBed(3,0);//无此房间
        } else return new GetBed(2,0);// 病人已在院
        return new GetBed(patientDao.insertPatient(new Patient(patientLiveIn.getId(),
                patientLiveIn.getName(),
                patientLiveIn.getSex(),
                patientLiveIn.getAge(),
                patientLiveIn.getTime(),
                patientLiveIn.getResult(),
                null,null)),bedNumber);
    }

    @Override
    public GetBed updatePatient(PatientLiveIn patientLiveIn) {
        Patient patient=patientDao.queryPatientByID(patientLiveIn.getId());
        String bedNumber = "0";
        if (patient!=null){
            In bed=patient.getBed();
            String[] s=patientLiveIn.getNumber().split("-");
            if (s[0]!=bed.getAreaCode()||s[1]!= bed.getRoomNumber()){// 是否转移房间
               Ward ward=wardDao.queryWardByAreaCode_RoomNumber(s[0],s[1]);
               if (ward!=null){
                   if (ward.getNumbers()>0){
                       // 搬出房间
                       wardDao.outWard(bed.getAreaCode(),bed.getRoomNumber(),bed.getBedNumber());
                       bed.setAreaCode(s[0]);
                       bed.setRoomNumber(s[1]);
                       if (ward.isEmpty1()){
                           bedNumber="1";
                           ward.setEmpty1(false);
                       }else if (ward.isEmpty2()){
                           bedNumber="2";
                           ward.setEmpty2(false);
                       }else if (ward.isEmpty3()){
                           bedNumber="3";
                           ward.setEmpty3(false);
                       }else return new GetBed(4,0);// 房间已满
                       ward.setNumbers(ward.getNumbers()-1);
                       wardDao.updateWard(ward);// 搬入新房间
                       bed.setBedNumber(bedNumber);
                       inDao.updateIn(bed);// 更新入住信息
                   }else return new GetBed(4,0);// 房间已满
               }else return new GetBed(3,0);// 要转移的房间不存在
            }
            return new GetBed(patientDao.updatePatient(new Patient(patientLiveIn.getId(),
                   patientLiveIn.getName(),
                   patientLiveIn.getSex(),
                   patientLiveIn.getAge(),
                   patientLiveIn.getTime(),
                   patientLiveIn.getResult(),
                   null,null)),Integer.parseInt(bedNumber));
        }else return new GetBed(2,0);// 查无此病人记录
    }


}
